﻿using System; 
using System.Collections.Generic;
using UnityEngine; 

namespace XFGameFramework.ScriptableObjectSystem
{
 
    /// <summary>
    /// 配表系统业务逻辑
    /// </summary>
    public class ScriptableObjectManager
    {

        #region 字段
         
        private static Dictionary<int, ScriptableObjectConfigs> configs = new Dictionary<int, ScriptableObjectConfigs>();
        
        private static Dictionary<string, int> string_to_hash = new Dictionary<string, int>();

        private static Dictionary<int, string> hash_to_string = new Dictionary<int, string>();

        #endregion


        #region 方法


        /// <summary>
        /// 添加配置
        /// </summary>
        /// <param name="config">配置</param>
        /// <exception cref="Exception">ID重复</exception>
        public static void AddConfig(ScriptableObjectConfigs config)
        {

            int name_hash = StringToHash(config.name);

            if (configs.ContainsKey(name_hash))
            {
                Debug.LogWarningFormat("已添加配置:{0},请勿重复添加!",config.name);
                return;
            }
            configs.Add(name_hash,config);
        }



        /// <summary>
        /// 移除配置
        /// </summary>
        /// <param name="config">配置对象</param>
        public static void RemoveConfig(ScriptableObjectConfigs config)
        { 
            RemoveConfig(config.name);
        }

        /// <summary>
        /// 移除配置
        /// </summary>
        /// <param name="name">配置名称</param>
        public static void RemoveConfig(string name) 
        {
            int name_hash = StringToHash(name);
            RemoveConfig(name_hash);
        }

        /// <summary>
        /// 移除配置
        /// </summary>
        /// <param name="name_hash">配置名称hash</param>
        public static void RemoveConfig(int name_hash)
        {
            if (configs.ContainsKey(name_hash))
            {
                configs.Remove(name_hash);
            }
        }


        /// <summary>
        /// 查询配置
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <returns></returns>
        public static ScriptableObjectConfigs GetConfig(string name)
        {
            int name_hash = StringToHash(name); 
            return GetConfig(name_hash);
        }


        /// <summary>
        /// 查询配置
        /// </summary>
        /// <param name="name_hash">配置名称hash</param>
        /// <returns></returns>
        public static ScriptableObjectConfigs GetConfig(int name_hash) 
        {
            if (configs.ContainsKey(name_hash))
                return configs[name_hash];

            return null;
        }


        /// <summary>
        /// 把字符串转成hash值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static int StringToHash(string str)
        {
            if (string_to_hash.ContainsKey(str))
                return string_to_hash[str];

            int hash = Animator.StringToHash(str);

            string_to_hash.Add(str, hash);

            if (!hash_to_string.ContainsKey(hash))
                hash_to_string.Add(hash, str);

            return string_to_hash[str];
        }
         
        #endregion

    }

}